<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Admin;
use App\Http\Model\Logs;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class LoginController extends Controller
{
    /**
     * 管理员登录
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login(){
        if($input=Input::all()){
            $verify=session('capt');
            if(strtolower($input['verify'])!=strtolower($verify)){
                return ['msg'=>'验证码错误','status'=>0];
            }
            $admin=Admin::where(['username'=>$input['username'],'password'=>$input['password']])->first();
            if($admin){
                Logs::create(['username'=>$input['username'],'event'=>'-管理员登录了后台']);
                session()->put(['admin'=>$admin]);
                return ['msg'=>"登陆成功",'status'=>1];
            }else{
                return ['msg'=>"用户名或者密码错误",'status'=>0];
            }
        }
        return view("admin.login");
    }

    /**
     * 验证码
     */
    public function verify(){
        $captcha=new CaptchaBuilder();
        $captcha->build('100','40',null);
        $capt=$captcha->getPhrase();
        //储存验证码
        Session::flash('capt',$capt);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $captcha->output();
    }

    /**
     * 管理员退出
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout(){
        Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-管理员退出了系统']);
        session(['admin'=>'']);
        return redirect('admin/login');
    }

    /**
     * 作者登录
     * @return $this|array
     */
    public function authorLogin(){
        if($input=Input::all()){
            $verify=session('capt');
            if(strtolower($input['verify'])!=strtolower($verify)){
                return ['msg'=>'验证码错误','status'=>0];
            }
            $admin=\App\Http\Model\User::where(['username'=>$input['username'],'password'=>
                $input['password'],'is_author'=>1])->first();
            if($admin){
                if($admin->status==1){
                    Logs::create(['username'=>$input['username'],'event'=>'-作者登录了后台']);
                    session()->put(['admin'=>$admin]);
                    return ['msg'=>"登陆成功",'status'=>1];
                }else if($admin->status==2){
                    return ['msg'=>"账号已停用",'status'=>0];
                }else{
                    return ['msg'=>"账号未激活",'status'=>0];
                }
            }else{
                return ['msg'=>"用户名或者密码错误",'status'=>0];
            }
        }
        return view('admin.author.login')->with(['title'=>'作者登录']);
    }

    /**
     * 作者退出登录
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function authorLogout(){
        Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-作者退出系统']);
        session(['admin'=>'']);
        return redirect('admin/authorlogin');
    }
}
